﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Microsoft.Practices.Unity;
using Exhibition.Common;
using Exhibition.Common.Helper;
using Exhibition.Common.Web;

namespace Exhibition
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            IUnityContainer container = new UnityContainer();
            ManagerWebInit.InitUnity(container);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            try
            {
                string path = Request.Path;
                string ext = System.IO.Path.GetExtension(path);
                if (!string.IsNullOrEmpty(ext) && !String.IsNullOrEmpty(ext.Trim().ToLower()))
                {
                    ext = ext.TrimStart('.').Trim().ToLower();
                }
                if (ext == "css" || ext == "js" || ext == "jpg" || ext == "bmp" || ext == "png" || ext == "gif")
                {
                    Server.ClearError();
                    return;
                }
                Exception ex = Server.GetLastError().GetBaseException();

                String error = "Error Caught in Application_Error event\r\n" +
                    "Error in:" + Request.Url.ToString() +
                    "\r\nError Message:" + ex.Message.ToString() +
                    "\r\nStack Trace:\r\n" + ex.StackTrace.ToString();
                LogHelper.Error(error);
            }catch(Exception ex){
                LogHelper.Error(ex.StackTrace);
            }
        }

        

        protected void Application_End(object sender, EventArgs e)
        {
            IUnityContainer applicationContainer = WebSingleton.ApplicationContainer;

            if (applicationContainer != null)
            {
                applicationContainer.Dispose();

                WebSingleton.ApplicationContainer = null;
            }
        }
    }
}